package com.team10.hotel.controller;

import com.team10.hotel.domain.Good;
import com.team10.hotel.domain.GoodOrder;
import com.team10.hotel.domain.Supplier;
import com.team10.hotel.service.GoodOrderService;
import com.team10.hotel.service.GoodService;
import com.team10.hotel.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

@Controller
@RequestMapping("/goodorder")
public class GoodOrderController {
    @Autowired
    private GoodOrderService goodOrderService;
    @Autowired
    private SupplierService supplierService;
    @Autowired
    private GoodService goodService;

    @GetMapping("/list")
    public String listGoodOrderPage(Model model) {
        List<GoodOrder> goodOrders = goodOrderService.listGoodOrder();
        model.addAttribute("goodOrders",goodOrders);
        return "goodOrderList";
    }

    //写一个

    @RequestMapping(value = "toAdd",method = RequestMethod.GET)
    public String add(Model model) {
        model.addAttribute("goodOrder",new GoodOrder());
        List<Good> goods=goodService.listGood();
        List<Supplier> suppliers=supplierService.listSupplier();
        model.addAttribute("goods",goods);
        model.addAttribute("suppliers",suppliers);
        return "goodOrderAdd";
    }

    @RequestMapping(value = "/findById")
    public String findGoodById(Model model,String id){
        GoodOrder goodOrder = goodOrderService.findGoodOrderById(id);
        List<Good> goods=goodService.listGood();
        List<Supplier> suppliers=supplierService.listSupplier();
        model.addAttribute("goodOrder",goodOrder);
        model.addAttribute("goods",goods);
        model.addAttribute("suppliers",suppliers);
        return "goodOrderAdd";
    }

    @RequestMapping(value = "/save")
    public String saveGoodOrder(GoodOrder goodOrder) {
        goodOrderService.saveOrUpdate(goodOrder);
        return "redirect:/goodorder/list";
    }

    @RequestMapping("/history")
    public String updateToHistory(String id){
        goodOrderService.updateGoodOrderStatusToHistory(id);
        return "redirect:/goodorder/list";
    }

    @RequestMapping("/delete")
    public String deleteGoodOrderById(String id){
        goodOrderService.deleteGoodOrderByPrimaryKey(id);
        return "redirect:/goodorder/toHistory";
    }

//    @RequestMapping("/reduction")
//    public String reductionGoodOrderById(String id){
//        goodOrderService.updateGoodOrderStatusToNow(id);
//        return "redirect:/goodorder/list";
//    }
    @RequestMapping("/toHistory")
    public String toHistory(Model model){
        List<GoodOrder> goodOrders =goodOrderService.listHistoryGoodOrder();
        model.addAttribute("goodOrders",goodOrders);
        return "goodOrderHistoryList";
    }
    @RequestMapping(value = "updateStatus")
    public String updateOrderStatus(String id){
        goodOrderService.updateGoodOrderStatusToNow(id);
        return "redirect:/goodorder/list";
    }
}



